package com.bjpowernode.controller;
import com.bjpowernode.beans.Activity;
import com.bjpowernode.beans.User;
import com.bjpowernode.dto.ResultDTO;
import com.bjpowernode.dto.ResultPageDTO;
import com.bjpowernode.exception.CRUDException;
import com.bjpowernode.services.ActivityServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@RestController
@RequestMapping("/Activity")
public class ActivityController {
    @Autowired
    ActivityServices activityServices;

    @RequestMapping("getAll.action")
    public  ResultPageDTO<Activity>  getAll(ResultPageDTO<Activity> activityResultPageDTO){
        /*
        -- 聚合查询(总行数count、最小min、最大max、平均avg,累加sum)
        -- select count(*)  from tbl_marketing_activities
        -- x从第几行开始（从0开始算第一行）（（当前页-1）*每页显示行数）
        -- y查询总行数
        select *  from tbl_marketing_activities
        limit  x,y
        -- 当前页、总行数、每页显示多少行
        -- 总页数计算：总行数/每页显示多少行  如果有余数，则(总行数/每页显示多少行)+1
         */
        ArrayList<Activity> all = activityServices.getAll(activityResultPageDTO);
        //控制器设置数据
        activityResultPageDTO.setData(all);
        return activityResultPageDTO;
    }




    @RequestMapping("getIdAndRemark.action")
    public Activity getIdAndRemark(String id){
        Activity activity = activityServices.get(id);
        return activity;
    }

    @RequestMapping("add.action")
    public ResultDTO add(Activity activity,HttpSession httpSession) throws CRUDException {
        //补充创建者
        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        User user =(User) httpSession.getAttribute("user");
        activity.setCreateBy(user.getName());
        boolean add = activityServices.add(activity);
        if(!add){
            throw new CRUDException("活动创建错误,影响行数为0");
        }

        return resultDTO;
    }


    @RequestMapping("del.action")
    public ResultDTO add(@RequestBody  String[] ids) throws CRUDException {
        //补充创建者
        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        boolean del = activityServices.del(ids);
        if(!del){
            throw new CRUDException("活动删除错误,影响行数为0");
        }
        return resultDTO;
    }




    @RequestMapping("editBefore.action")
    public Activity editBefore(String id){
        Activity activity = activityServices.getEditBefore(id);
        return activity;
    }


    @RequestMapping("edit.action")
    public ResultDTO edit(Activity activity,HttpSession httpSession) throws CRUDException {

        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        //补充创建者
        User user =(User) httpSession.getAttribute("user");
         activity.setEditBy(user.getName());
        boolean edit = activityServices.edit(activity);
        if(!edit){
            throw new CRUDException("活动修改错误,影响行数为0");
        }
        return resultDTO;
    }





}
